﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;
using HYElectricSystemDiagram.DataStruct;

namespace HYElectricSystemDiagram.FormSet
{
	public partial class FormSystemDiagramSet : HYBaseForm
	{
		public FormSystemDiagramSet(string name, ExternalCommandData m_commandData = null)
		{
			this.InitializeComponent();
			this.treeViewSet.ExpandAll();
			this.commandData = m_commandData;
			this.treeViewSet.HideSelection = false;
			this.treeViewSet.DrawMode = TreeViewDrawMode.OwnerDrawText;
			this.treeViewSet.DrawNode += this.treeViewSet_DrawNode;
			this.SelectTreeView(this.treeViewSet, name);
			string text = this.treeViewSet.SelectedNode.Text;
			this.groupBoxUser.Controls.Clear();
			if (!text.Contains("住户"))
			{
				this.set1 = new UserControlSet1(text);
				this.groupBoxUser.Controls.Add(this.set1);
				return;
			}
			this.set2 = new UserControlSet2(text, this.commandData);
			this.groupBoxUser.Controls.Add(this.set2);
		}

		private void treeViewSet_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			e.DrawDefault = true;
		}

		private void SelectTreeView(TreeView treeView, string selectStr)
		{
			treeView.Focus();
			for (int i = 0; i < treeView.Nodes.Count; i++)
			{
				for (int j = 0; j < treeView.Nodes[i].Nodes.Count; j++)
				{
					if (treeView.Nodes[i].Nodes[j].Text == selectStr)
					{
						treeView.SelectedNode = treeView.Nodes[i].Nodes[j];
						treeView.Nodes[i].Expand();
						return;
					}
				}
			}
		}

		private void btnCanel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void treeViewSet_MouseClick(object sender, MouseEventArgs e)
		{
			TreeNode nodeAt = this.treeViewSet.GetNodeAt(e.X, e.Y);
			string text = nodeAt.Text;
			this.strSelectItem = text;
			if (text.Equals("动力系统图") || text.Equals("住户配电系统图"))
			{
				return;
			}
			this.groupBoxUser.Controls.Clear();
			if (!text.Equals("住户配电箱"))
			{
				this.set1 = new UserControlSet1(nodeAt.Text);
				this.groupBoxUser.Controls.Add(this.set1);
				return;
			}
			this.set2 = new UserControlSet2(nodeAt.Text, this.commandData);
			this.groupBoxUser.Controls.Add(this.set2);
		}

		private void BtnEnter_Click(object sender, EventArgs e)
		{
			if (!this.treeViewSet.SelectedNode.Text.Equals("住户配电箱"))
			{
				this.set1.UpdateXmlHisData();
				this.userData = this.set1.GetWindowsData();
			}
			else
			{
				this.set2.UpdateXmlHisData();
				this.userDataHouse = this.set2.GetWindowsData();
			}
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void btnUsed_Click(object sender, EventArgs e)
		{
			if (!this.treeViewSet.SelectedNode.Text.Equals("住户配电箱"))
			{
				this.set1.UpdateXmlHisData();
				return;
			}
			this.set2.UpdateXmlHisData();
		}

		public string strSelectItem = "";

		private UserControlSet1 set1;

		private UserControlSet2 set2;

		public UserControlData1 userData;

		public UserControlData2 userDataHouse;

		private ExternalCommandData commandData;
	}
}
